﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using Leadtools.DicomDemos;

namespace DICOMViewer.Utility
{
    [Serializable]
    public class ClientConfig
    {
        public string AETitle { get; set; }
        public int Port { get; set; }

        [XmlElement(ElementName = "Servers")]
        public List<ServerInfo> Servers { get; set; }
    }

    [Serializable]
    public class ServerInfo:ICloneable
    {
        public string AETitle { get; set; }
        public string Ip { get; set; }
        public int Port { get; set; }

        public object Clone()
        {
            return new ServerInfo()
            {
                AETitle = this.AETitle,
                Port = this.Port, 
                Ip = this.Ip
            };
        }
    }

    public abstract class ComSettingReader
    {
        private static ClientConfig clientConfig;
        private static List<DicomServer> listDicomServer;

        public static ClientConfig getClientConfig()
        {
            if (clientConfig == null)
            {
                clientConfig =
                    Utility.SerializeHelper.DeserializeWithXmlFile<ClientConfig>(Application.StartupPath +
                                                                                 "\\comsetting.xml");
                if (clientConfig.Servers ==null && string.IsNullOrEmpty(clientConfig.AETitle))
                {
                    InitClientConfig();
                }
            }
            return clientConfig;
        }

        public static DicomServer[] getServerConfig()
        {
            List<DicomServer> list = new List<DicomServer>();
            foreach (ServerInfo info in getClientConfig().Servers)
            {
                DicomServer server = new DicomServer()
                {
                    AETitle = info.AETitle,
                    Address = IPAddress.Parse(info.Ip),
                    Port = info.Port,
                    IpType = Leadtools.Dicom.DicomNetIpTypeFlags.Ipv4,
                    Timeout = 30
                };
                list.Add(server);
            }
            listDicomServer = list;
            return listDicomServer.ToArray();
        }

        public static void SaveClientConfig(ClientConfig config)
        {
            clientConfig = config;
            Utility.SerializeHelper.SerializeToXmlFile(clientConfig, Application.StartupPath + "\\comsetting.xml");
        }

        public static void SaveServerConfig(ServerInfo[] servers)
        {
            clientConfig.Servers = servers.ToList();
            SaveClientConfig(clientConfig);
        }

        private static ClientConfig InitClientConfig()
        {
            ClientConfig config = new ClientConfig()
            {
                AETitle = "Client1",
                Port = 1000,
                Servers = new List<ServerInfo>()
                {
                    new ServerInfo()
                    {
                        AETitle = "LocalServer",
                        Port = 104,
                        Ip = "127.0.0.7"
                    }
                }
            };
            SaveClientConfig(config);
            return config;
        }
    }
}